/* 子应用 */
const express=require('express');
const query=require('../pool');
const coupon=express.Router();
const jwt=require('../modules/token.js')

//存优惠券
coupon.post('/',async(req,res)=>{
  let obj = req.body  //获取请求的数据
  let startAt=obj.date
  let sql = `insert into user_laptop (uid,lid,startAt) values (?,?,?)`
  let params = [obj.uid,obj.lid,obj.date];  //参数
  let result = await query(sql,params)
  res.send({code:0,msg:'优惠券存入成功'})
});

// 获取优惠券
coupon.get('/get?:uid',async(req,res)=>{
  let obj = req.query  //获取请求的数据
  let uid=jwt.verifytoken(obj[0]);
  if(!uid){
    res.send({code:0,msg:'token无效'})
  }else{
    let sql = `select laptop.discount,laptop.denominations,laptop.originCondition,laptop.name,laptop.description,laptop.startAt,laptop.endAt,user_laptop.yid,user_laptop.isUse from laptop,user_laptop where laptop.lid=user_laptop.lid and user_laptop.uid=?;`
    let params = [uid.uid];  //参数
    let result = await query(sql,params)
    res.send({code:1,msg:'获取优惠券数据成功',data:result})
  }
});

// 使用优惠券
coupon.get('/del',async(req,res)=>{
  let obj=req.query;
//console.log(obj);
  let result=await query(`delete from user_laptop where yid=${obj.yid*1}`)
//console.log(result);
})

//暴露
module.exports = coupon;